package com.kxmall.web.controller.huishouapp;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.kxmall.common.annotation.Log;
import com.kxmall.common.annotation.RepeatSubmit;
import com.kxmall.common.core.controller.BaseAppController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.validate.AddGroup;
import com.kxmall.common.enums.BusinessType;
import com.kxmall.huishou.domain.bo.HsActivityJoinBo;
import com.kxmall.huishou.domain.bo.HsUserSignBo;
import com.kxmall.huishou.domain.bo.HsUserTaskBo;
import com.kxmall.huishou.domain.bo.HsWasteActivityBo;
import com.kxmall.huishou.domain.vo.HsUserSignVo;
import com.kxmall.huishou.domain.vo.HsUserTaskVo;
import com.kxmall.huishou.domain.vo.HsWasteActivityVo;
import com.kxmall.huishou.service.IHsActivityJoinService;
import com.kxmall.huishou.service.IHsUserSignService;
import com.kxmall.huishou.service.IHsUserTaskService;
import com.kxmall.huishou.service.IHsWasteActivityService;
import com.kxmall.user.domain.vo.KxUserVo;
import com.kxmall.web.controller.user.service.IKxAppUserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 成长中心
 *
 * @author lf
 * @date 2023-12-01
 */

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/growth/app")
public class HsAppGrowthCenterController extends BaseAppController {

    private final IHsUserTaskService iHsUserTaskService;
    private final IHsUserSignService iHsUserSignService;




    /**
     * 获取签到信息
     *
     * @param
     */
    @GetMapping("/getSignInfo")
    public R<Map<String,Object>> getSignInfo() {
        Map<String,Object> dataMap = new HashMap<>();
        Long userId = getAppLoginUser().getUserId();
        Calendar calendar = Calendar.getInstance();
        int nowDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        for(int i = 1; i <= 7; i++){
            if(i > nowDayOfWeek){
                dataMap.put((i-1)+"", -1);
                continue;
            }
            int days = nowDayOfWeek - i;
            String currentDate = DateFormatUtils.format(System.currentTimeMillis()-(3600*1000l*24*days), "yyyy-MM-dd");

            HsUserSignBo hsUserSignBo = new HsUserSignBo();
            hsUserSignBo.setUserId(userId);
            hsUserSignBo.setSignDate(currentDate);
            List<HsUserSignVo> dataList = iHsUserSignService.queryList(hsUserSignBo);
            if(dataList != null && dataList.size() >0){
                dataMap.put((i-1)+"", 1);
            }else {
                if(i == nowDayOfWeek){
                    dataMap.put((i-1)+"", 0);
                }else {
                    dataMap.put((i-1)+"", 2);
                }

            }

        }


        return R.ok(dataMap);
    }

    /**
     * 查询用户签到记录列表
     */
    @GetMapping("/userSignList")
    public TableDataInfo<HsUserSignVo> list( PageQuery pageQuery) {
        if(pageQuery != null){
            pageQuery.setIsAsc("desc");
            pageQuery.setOrderByColumn("create_time");
        }
        Long userId = getAppLoginUser().getUserId();
        HsUserSignBo hsUserSignBo = new HsUserSignBo();
        hsUserSignBo.setUserId(userId);

        return iHsUserSignService.queryPageList(hsUserSignBo, pageQuery);
    }



    @Log(title = "新增用户签到记录", businessType = BusinessType.INSERT)
    @GetMapping("/add")
    public R<Void> add() {
        Long userId = getAppLoginUser().getUserId();
        HsUserSignBo hsUserSignBo = new HsUserSignBo();
        hsUserSignBo.setUserId(userId);
        hsUserSignBo.setSignDate(DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd"));
        List<HsUserSignVo> dataList = iHsUserSignService.queryList(hsUserSignBo);
        if(dataList != null && dataList.size() >0){
            return R.fail("今日已签到");
        }
        hsUserSignBo.setUserName(getAppLoginUser().getUsername());

        return toAjax(iHsUserSignService.insertByBo(hsUserSignBo));
    }



    /**
     * 查询成就任务列表
     */
    @GetMapping("/userTaskList")
    public TableDataInfo<HsUserTaskVo> getUserTaskList(HsUserTaskBo bo, PageQuery pageQuery) {
        if(pageQuery != null){
            pageQuery.setIsAsc("desc");
            pageQuery.setOrderByColumn("create_time");
        }

        return iHsUserTaskService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询成就任务详情
     */
    @GetMapping("/userTaskById/{id}")
    public R<HsUserTaskVo> userTaskById(@NotNull(message = "主键不能为空")@PathVariable Long id) {

        return R.ok(iHsUserTaskService.queryById(id));
    }









}
